/*
 * Copyright (C) 2012 SeekDeepWithin.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.seekdeepwithin.Controls;

import com.seekdeepwithin.*;
import com.seekdeepwithin.Data.Book;
import com.seekdeepwithin.Data.ITab;
import com.seekdeepwithin.DataAccess.*;
import java.io.*;
import java.util.*;
import javafx.beans.property.*;
import javafx.event.*;
import javafx.fxml.*;
import javafx.geometry.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;

/**
 * User: Jonathan Montiverdi Date: 10/1/12
 *
 * Library control for seek deep within.
 */
public class LibraryControl extends VBox implements ITab
{
   @FXML
   private VBox _LibraryItems;
   @FXML
   private ScrollPane _ScrollPane;
   private StringProperty tabTitle = new SimpleStringProperty ();
   private ObjectProperty<Tooltip> tabToolTip = new SimpleObjectProperty<> ();

   /**
    * Initializes a new library control.
    */
   @SuppressWarnings ("LeakingThisInConstructor")
   public LibraryControl ()
   {
      this.tabTitle.set ("Online Library");
      this.tabToolTip.set (new Tooltip ("View the list of books on the server."));
      FXMLLoader fxmlLoader = new FXMLLoader (getClass ().getResource ("Library.fxml"));
      fxmlLoader.setRoot (this);
      fxmlLoader.setController (this);
      try
      {
         fxmlLoader.load ();
      }
      catch (IOException ex)
      {
         throw new RuntimeException (ex);
      }
      this.LoadBooks ();
   }

   /**
    * Gets the title of the tab.
    * @return The title of the tab.
    */
   @Override
   public String getTabTitle ()
   {
      return this.tabTitle.get ();
   }

   /**
    * Gets the title of the tab property.
    * @return Tab title property.
    */
   @Override
   public StringProperty tabTitleProperty ()
   {
      return this.tabTitle;
   }

   /**
    * Gets the tool tip of the tab.
    * @return The tool tip of the tab.
    */
   @Override
   public Tooltip getTabToolTip ()
   {
      return this.tabToolTip.get ();
   }

   /**
    * Gets the tool tip of the tab property.
    * @return Tab tool tip property.
    */
   @Override
   public ObjectProperty<Tooltip> tabToolTipProperty ()
   {
      return this.tabToolTip;
   }

   /**
    * Loads the books from the main database.
    */
   private void LoadBooks ()
   {
      ArrayList <Book> books = SdwApi.getInstance ().getBookList ();
      for (final Book book : books)
      {
         Region spacer = new Region();
         HBox.setHgrow(spacer, Priority.ALWAYS);
         Text info = TextBuilder.create ().text (book.getInfo ()).id ("libraryInfo").build ();
         info.wrappingWidthProperty ().bind (this._ScrollPane.widthProperty ().subtract (25));
         this._LibraryItems.getChildren ().add (VBoxBuilder.create ()
                 .spacing (5)
                 .padding (new Insets (0,0,0,10))
                 .children (HBoxBuilder.create ()
                     .id ("libraryTitleBox")
                     .padding (new Insets (0, 5, 0, 0))
                     .alignment (Pos.CENTER_LEFT)
                     .children (LabelBuilder.create ()
                           .text (book.getTitle ())
                           .id ("libraryTitle")
                           .build (),
                        spacer,
                        ButtonBuilder.create ()
                           .text ("Download")
                           .id ("rich-blue")
                           .onAction (new EventHandler<ActionEvent> () {
                              @Override
                              public void handle (ActionEvent t) {
                                 MainController.getInstance ().queueJob (new ImportBookService (book));
                              }
                           })
                           .build ())
                        .build (),
                     LabelBuilder.create ()
                        .text (book.getDate ())
                        .id ("libraryDate")
                        .build (),
                     info
                 )
                 .build ()
         );
      }
   }

   /**
    * Occurs when the tab is closing.
    */
   @Override
   public void closing ()
   {
   }
}
